package com.qeegoo.b2bautozityp.moudle.order.viewmodel;

//┏┓　　　┏┓ 
//┏┛┻━━━┛┻┓ 
//┃　　　　　　　┃ 　 
//┃　　　━　　　┃ 
//┃　┳┛　┗┳　┃ 
//┃　　　　　　　┃ 
//┃　　　┻　　　┃ 
//┃　　　　　　　┃ 
//┗━┓　　　┏━┛ 
//┃　　　┃  神兽保佑　　　　　　　　 
//┃　　　┃  代码无BUG！ 
//┃　　　┗━━━┓ 
//┃　　　　　　　┣┓ 
//┃　　　　　　　┏┛ 
//┗┓┓┏━┳┓┏┛ 
// ┃┫┫　┃┫┫ 
// ┗┻┛　┗┻┛

import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.databinding.ObservableList;
import android.view.View;

import com.kelin.mvvmlight.command.ReplyCommand;
import com.qeegoo.b2bautozityp.BR;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.api.HttpParams;
import com.qeegoo.b2bautozityp.api.RequestApi;
import com.qeegoo.b2bautozityp.api.RetrofitService;
import com.qeegoo.b2bautozityp.moudle.order.model.OrderResultBean;
import com.qeegoo.b2bautozityp.widget.OrderDeliveryDialog;
import com.qeegoo.b2bautozityp.widget.OrderDeliveryDialog.OnDialogFinishListener;

import java.util.List;

import base.lib.util.ActivityManager;
import base.lib.util.ToastUtils;
import me.tatarka.bindingcollectionadapter.ItemView;

/**
 * Created by lk on 2017/9/18.
 */

public class OrderDetailViewModel {

    private OrderDeliveryDialog mDialog;
    private final RequestApi requestApi;
    public ObservableList<OrderItemViewModel> items = new ObservableArrayList<>();
    public ItemView itemView = ItemView.of(BR.viewModel, R.layout.adapter_detail_order);

    public ObservableField<String> orderStatus = new ObservableField<>();
    public ObservableField<String> orderStatusMsg = new ObservableField<>();
    public ObservableField<String> orderReturnMoeny = new ObservableField<>();
    public ObservableField<String> depotInfo = new ObservableField<>();
    public ObservableField<String> depotDetailAddress = new ObservableField<>();
    public ObservableField<String> personInfo = new ObservableField<>();
    public ObservableField<String> personDetailAddress = new ObservableField<>();
    public ObservableField<String> goodsCount = new ObservableField<>();
    public ObservableField<String> goodsPrice = new ObservableField<>();
    public ObservableField<String> remarks = new ObservableField<>();
    public ObservableField<String> orderId = new ObservableField<>();
    public ObservableField<String> bookGoodTime = new ObservableField<>();
    public ObservableField<String> payTime = new ObservableField<>();
    public ObservableField<Integer> addressVisible = new ObservableField<>(View.VISIBLE);
    public ObservableField<Boolean> callBKVisible = new ObservableField<>(false);

    public OrderDetailViewModel(RequestApi requestApi) {
        this.requestApi = requestApi;
    }

    //通知百库来取货
    public ReplyCommand callBKCommand = new ReplyCommand<>(() ->{
        mDialog = new OrderDeliveryDialog(ActivityManager.getActivity(), new OnDialogFinishListener() {
            @Override
            public void onFinish() {
                if (mDialog != null){
                    mDialog.dismiss();
                    new View(ActivityManager.getActivity()).postDelayed(()->{
                        ActivityManager.getActivity().finish();
                    },1000);
                }
            }

            @Override
            public void onCancel() {

            }
        });
        mDialog.show();
    });

    public void getDatas(String code) {
        requestApi.orderView(HttpParams.paramViewByCode(code))
                .map(new RetrofitService.HttpResultFunc<>())
                .compose(RetrofitService.applySchedulers())
                .subscribe(orderDetailBean -> {

                    OrderResultBean.OrderListBean bean = orderDetailBean.orderHeader;
                    orderStatus.set(bean.orderStatusText);
                    personInfo.set(bean.customer +"  "+bean.customerTel);
                    personDetailAddress.set(bean.orderAddress);
                    callBKVisible.set("120".equals(bean.orderStatus));

                    orderId.set("订单号："+bean.code);
                    remarks.set("remarks");
                    bookGoodTime.set("订货时间："+"2017-9-22 12：12");
                    payTime.set("交货时间："+"2017-9-22 12：12");
                    goodsCount.set("商品共"+"2"+"件");
                    goodsPrice.set("¥"+bean.totalMoney);
                    items.clear();
                    List<OrderResultBean.Orderben> orders = bean.orders;
                    for (OrderResultBean.Orderben orderben : orders){
                        items.add(new OrderItemViewModel(orderben));
                    }
                },throwable -> {
                    ToastUtils.showToast(throwable.getMessage());
                });
    }

    public void getDatas1(String code) {
        requestApi.orderReturnView(HttpParams.paramViewByCode(code))
                .map(new RetrofitService.HttpResultFunc<>())
                .compose(RetrofitService.applySchedulers())
                .subscribe(orderDetailBean -> {

                    OrderResultBean.OrderListBean bean = orderDetailBean.orderHeader;
                    orderStatus.set(bean.orderStatusText);
                    personInfo.set(bean.customer +"  "+bean.customerTel);
                    personDetailAddress.set(bean.orderAddress);

                    orderId.set("订单号："+bean.code);
                    remarks.set("remarks");
                    bookGoodTime.set("订货时间："+"2017-9-22 12：12");
                    payTime.set("交货时间："+"2017-9-22 12：12");
                    goodsCount.set("商品共"+"2"+"件");
                    goodsPrice.set("¥"+bean.totalMoney);
                    items.clear();
                    List<OrderResultBean.Orderben> orders = bean.orders;
                    for (OrderResultBean.Orderben orderben : orders){
                        items.add(new OrderItemViewModel(orderben));
                    }
                    addressVisible.set(View.GONE);
                },throwable -> {
                    ToastUtils.showToast(throwable.getMessage());
                });
    }
}
